﻿/*
 * GLLogicManager.h
 *
 *  Created on: Jun 12, 2014
 *      Author: yuliang
 */

#ifndef GLLOGICMANAGER_H_
#define GLLOGICMANAGER_H_

#include "GLLogicBase.h"
#include "common/PSingleton.h"
#include <list>

class CGLLogicManager : public CSingleton<CGLLogicManager> {
	typedef std::list<CGLLogicBase *> ListLogic_t;
public:
	CGLLogicManager();
	virtual ~CGLLogicManager();
	inline void RegisterLogic(CGLLogicBase *logic) {
		if (!logic->HasRegister())
			m_Logics.push_back(logic);
		logic->Register();
	}
	inline void HandleLogic(char *data, uint32_t len, void *connect) {
		if (len >= m_PkgMinLen && len <= PROTOCOL_PACKAGE_MAX_LEN) {
			const ProtocolBase* tPkgBase = (const ProtocolBase*)data;
			handleLogic(tPkgBase, data, len, connect);
		}
	}
	inline void Disconnect(void *connect) {
		
	}
private:
	inline void handleLogic(const ProtocolBase *base, char * data, uint32_t len, void *connect) {
		for (ListLogic_t::iterator it = m_Logics.begin(); it != m_Logics.end(); ++it) {
			CGLLogicBase * tLogic = *it;
			if (tLogic->CatchLogic(base, data, len, connect))
				break;
		}
	}
private:
	static uint16_t				m_PkgMinLen;
	ListLogic_t					m_Logics;
};

#endif /* GLLOGICMANAGER_H_ */
